package com.sneaker.shower.socket.repository;

import com.sneaker.shower.socket.feature.SignAble;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <h1>ListRepository</h1>
 * <pre>
 * @Description 列表仓库
 * </pre>
 *
 * @Author lopo
 * @Date 2018/10/17
 */
public class ListRepository<T extends SignAble> implements Repository<T> {
    private final Map<Serializable, SignAble> ref = new ConcurrentHashMap<>(Repository.SIZE);

    @Override
    public T save(T channel) {
        ref.put(channel.sign(),channel);
        return channel;
    }

    @Override
    public T get(SignAble sign) {
        return (T) ref.get(sign.sign());
    }

    @Override
    public void delete(SignAble sign) {
         ref.remove(sign.sign());
    }

    @Override
    public Collection<T> peek(Predicate<T> predicate) {
        if(predicate ==null){
            return (Collection<T>) ref.values();
        }
        return (Collection)ref.values().stream().filter((Predicate)predicate).collect(Collectors.toList());
    }

    protected Map<Serializable, SignAble> getRef() {
        return ref;
    }
}
